<?php
include '../../../config.php';

if($_GET['gallery_section'] > 0) //gallery mode
{
	if(GALLERY_MODE == 'dir') //we can create dirs
	{
		$section = mysql_fetch_array(mysql_query("SELECT * FROM ".DB_PREFIX."yaps_gallery_sections WHERE id = ".$_GET['gallery_section']));
		$uploaddir = '../../../'.GALLERY_DIR.$section['dir'];

		if(!is_dir($uploaddir)) //cant find dir, lets try to create it :)
		{
			$res = mkdir($uploaddir,0777);
		}

		$ext = explode('.',basename($_FILES['Filedata']['name']));
		$ext[1] = strtolower($ext[1]);
		$name = time();
		$file_for_upload = $uploaddir . $name .'.'. $ext[1]; //random name
		$file = $section['dir'] . $name .'.'. $ext[1]; //random name
		$thumb = $section['dir'] . $name.'_thumb.'.$ext[1];
	}
	else //we cant create dirs
	{
		$section = mysql_fetch_array(mysql_query("SELECT * FROM ".DB_PREFIX."yaps_gallery_sections WHERE id = ".$_GET['gallery_section']));

		$ext = explode('.',basename($_FILES['Filedata']['name']));
		$ext[1] = strtolower($ext[1]);
		$name = substr($section['dir'],0,-1).'.'.time();

		$file_for_upload = '../../../' . GALLERY_DIR . $name .'.'. $ext[1]; //random name
		$file = GALLERY_DIR . $name .'.'. $ext[1]; //random name
		$thumb = GALLERY_DIR . $name .'_thumb.'. $ext[1];

	}

}
else //upload mode
{
	$file_for_upload = '../../../..' . $_REQUEST['folder'] . basename($_FILES['Filedata']['name']);
}

$handler = fopen('log.txt','a');
fwrite($handler,$file_for_upload."\n\n".print_r($_FILES,true));
fclose($handler);

$size=$_FILES['Filedata']['size'];
if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_for_upload))
{
  echo "success";

	$handler = fopen('log.txt','a');
	fwrite($handler,'nahrano!');
	fclose($handler);

	if($_GET['gallery_section'] > 0) //gallery mode
	{
		createthumb($file_for_upload,'../../../'.$thumb,GALLERY_THUMB_WIDTH,GALLERY_THUMB_HEIGHT);
		createthumb($file_for_upload,'../../../'.$file,GALLERY_MAX_WIDTH,GALLERY_MAX_HEIGHT);

		//protection against multiple insertion
		$row = mysql_fetch_array(mysql_query("SELECT * FROM ".DB_PREFIX."yaps_gallery_images ORDER BY id DESC LIMIT 1"));
		if($row['file'] != $file && $row['thumb'] != $thumb)
		{
			mysql_query("INSERT INTO ".DB_PREFIX."yaps_gallery_images VALUES('0','".$file."','".$thumb."','','','".$_GET['gallery_section']."','0')");
			mysql_query("UPDATE ".DB_PREFIX."yaps_gallery_sections SET photo_count = photo_count + 1 WHERE id = ".$_GET['gallery_section']);
		}
	}
	else //upload mode
	{
		$check = mysql_query("SELECT * FROM ".DB_PREFIX."yaps_uploads WHERE name = '".addslashes($_FILES['Filedata']['name'])."'");
		if(mysql_num_rows($check) < 1)
		{
			$query = mysql_query("INSERT INTO ".DB_PREFIX."yaps_uploads VALUES('0','".addslashes($_FILES['Filedata']['name'])."',NOW())");
		}
	}
}
else
{
	echo "error ".$_FILES['uploadfile']['error']." --- ".$_FILES['Filedata']['tmp_name']." %%% ".$file."($size)";

	$handler = fopen('log.txt','a');
	fwrite($handler,"\n\nerror ".$_FILES['Filedata']['error']." --- ".$_FILES['Filedata']['tmp_name']."---> ".$_FILES['Filedata']['name']." %%% ".$file."($size)");
	fclose($handler);
}

function createthumb($name,$filename,$new_w,$new_h)
{
	$ext = substr($name, -5);
	if (preg_match("/jpg|jpeg/",$ext)){$src_img=imagecreatefromjpeg($name);}
	if (preg_match("/png/",$ext)){$src_img=imagecreatefrompng($name);}
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	if ($old_x > $old_y)
	{
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
	}
	if ($old_x < $old_y)
	{
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y)
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
	if (preg_match("/png/",$ext))
	{
		imagepng($dst_img,$filename);
	} else {
		imagejpeg($dst_img,$filename);
	}
	imagedestroy($dst_img);
	imagedestroy($src_img);
}
?>